package servlet.books;

import com.alibaba.fastjson.JSONObject;
import utils.JdbcUtil;
import utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 添加图书
 */
@WebServlet(urlPatterns = "/booksAdd")
public class BooksAdd extends HttpServlet {

    JdbcUtil jdbcUtil = new JdbcUtil();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取参数
        String no = req.getParameter("no");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String author = req.getParameter("author");
        Integer price = Integer.valueOf(req.getParameter("price"));
        String language = req.getParameter("language");
        //打开连接
        Connection connection = jdbcUtil.getConnection();
        //插入语句
        String sql = "insert into book_manage(no, name, type, author, price, language) values(?, ?, ?, ?, ?, ?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultData resultData;
            if (no != null && no.trim().length() != 0) {
                preparedStatement.setString(1, no);
            } else {
                resultData = ResultData.buildMsg(1, "编号不能为空");
            }
            if (name != null && name.trim().length() != 0) {
                preparedStatement.setString(2, name);
            } else {
                resultData = ResultData.buildMsg(1, "名字不能为空");
            }
            if (type != null && type.trim().length() != 0) {
                preparedStatement.setString(3, type);
            } else {
                resultData = ResultData.buildMsg(1, "分类不能为空");
            }
            if (author != null && author.trim().length() != 0) {
                preparedStatement.setString(4, author);
            } else {
                resultData = ResultData.buildMsg(1, "作者不能为空");
            }
            if (price != null) {
                preparedStatement.setInt(5, price);
            } else {
                resultData = ResultData.buildMsg(1, "作者不能为空");
            }
            if (language != null && language.trim().length() != 0) {
                preparedStatement.setString(6, language);
            } else {
                resultData = ResultData.buildMsg(1, "语言不能为空");
            }
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                resultData = ResultData.success();
            } else {
                resultData = ResultData.fail();
            }
            Object o = JSONObject.toJSON(resultData);
            resp.getWriter().print(o.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

